#ifndef GOOLNN_CANGYAN_WINDOW_IAMGESMANAGER
#define GOOLNN_CANGYAN_WINDOW_IAMGESMANAGER

#include <QContextMenuEvent>
#include <QStringList>
#include <QScrollArea>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QWidget>
#include <QAction>
#include <QMenu>

#include "tool/Byte.hpp"
#include "file/Image.hpp"

class ImagesManager : public QWidget{
    Q_OBJECT

    private:
        const Byte ROW_SIZE = 6;
        const Byte INDEX_SIZE = 30;
        const Byte SPACE_SIZE = 20;

        QScrollArea* parent;

        QList<QRect> rects;

        QList<Byte> focusList;
        Byte hoverIndex;

        // 拖动选区参数
        bool mouseHoding;
        bool draging;

        int mousePressX;
        int mousePressY;
        int mouseMoveX;
        int mouseMoveY;

        // 图片显示参数
        Byte showingIndex;

        // 图片缩放
        float scale;

        // 显示坐标
        float viewX;
        float viewY;

        // 图像坐标
        float imageX;
        float imageY;

        // 图像宽高
        float imageWidth;
        float imageHeight;

        // 鼠标坐标
        int mouseX;
        int mouseY;

        float oldViewX;
        float oldViewY;

        bool moving;

        QMenu* contextMenu;

        QAction* viewImageAction;

        QAction* addImagesAction;
        QAction* removeImagesAction;

        QAction* insertAtPreviousAction;
        QAction* insertAtNextAction;
        
        QAction* clearImagesAction;

        bool inArea(int x, int y);

    public:
        QStringList files;
        // QList<QImage> images;
        QList<Image> images;

        ImagesManager(QScrollArea* parent);

        ~ImagesManager();

    protected:
        void paintEvent(QPaintEvent*);

        void mousePressEvent(QMouseEvent* event);
        void mouseReleaseEvent(QMouseEvent* event);

        void mouseDoubleClickEvent(QMouseEvent*);

        void mouseMoveEvent(QMouseEvent* event);

        void wheelEvent(QWheelEvent*);

        void leaveEvent(QEvent*);

        void contextMenuEvent(QContextMenuEvent*);

        void resizeEvent(QResizeEvent*);
    
    private slots:
        void viewImage();

        void addImages();
        void removeImages();
        void clearImages();

        void insertAtPrevious();
        void insertAtNext();

        bool eventFilter(QObject* target, QEvent* event);

};

#endif
